package com.ftg.learn.springboot.contact.controller;

import com.ftg.learn.springboot.contact.pojo.opinion;
import com.ftg.learn.springboot.contact.service.opinionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("contact")
public class opinionController {

	@Autowired
	opinionService os;

	@PostMapping("/addContact")
	public int addContact(opinion opinion){

		opinion.setConid(UUID.randomUUID().toString());
		List<opinion> list = os.showall(opinion.getConname());

		if(list.size() == 0){

			return os.addController(opinion);

		}else if(list.size() > 0){
			for(int i=0;i < list.size() ;i++){

				if(list.get(i).getConemail().equals(opinion.getConemail()) && list.get(i).getConhelp().equals(opinion.getConhelp())){

					return 0;
				}
			}
		}



		return os.addController(opinion);
	}
}
